package com.kuanter.kuanterauto.website;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.RouteOverlay;
import com.baidu.mapapi.TransitOverlay;

import com.kuanter.kuanterauto.HomeActivityGroup;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.umeng.analytics.MobclickAgent;

public class WebSiteRouteActivity extends MapActivity {
//	BMapManager mBMapMan = null;

	MapView mMapView = null;	// 地图View
	MKSearch mSearch = null;	// 搜索模块，也可去掉地图模块独立使用
	ProgressDialog progressDialog=null;
	Button btTopLeft_wsr = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.websiteroute);
		
//		mBMapMan = new BMapManager(this);
//		mBMapMan.init("755CDC09D6ACCAD8274A01D02576948F7904B7A1",
//				new MyGeneralListener());
		HomeActivityGroup.mBMapMan.start();
        // 如果使用地图SDK，请初始化地图Activity
        super.initMapActivity(HomeActivityGroup.mBMapMan);
        
        
		
        mMapView = (MapView)findViewById(R.id.bmapsView2);
        mMapView.setBuiltInZoomControls(true);
        //设置在缩放动画过程中也显示overlay,默认为不绘制
        mMapView.setDrawOverlayWhenZooming(true);
        showProgress("正在获取行车路线...");
        // 初始化搜索模块，注册事件监听
        mSearch = new MKSearch();
        mSearch.init(HomeActivityGroup.mBMapMan, new MKSearchListener(){

            @Override
            public void onGetPoiDetailSearchResult(int type, int error) {
            }
            
			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
				// 错误号可参考MKEvent中的定义
				if (error != 0 || res == null) {
					Toast.makeText(WebSiteRouteActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					finishProgress();
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(WebSiteRouteActivity.this, mMapView);
			    // 此处仅展示一个方案作为示例
			    routeOverlay.setData(res.getPlan(0).getRoute(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    finishProgress();
			    mMapView.invalidate();
			    
			    mMapView.getController().animateTo(res.getStart().pt);
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(WebSiteRouteActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					return;
				}
				TransitOverlay  routeOverlay = new TransitOverlay (WebSiteRouteActivity.this, mMapView);
			    // 此处仅展示一个方案作为示例
			    routeOverlay.setData(res.getPlan(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    mMapView.invalidate();
			    
			    mMapView.getController().animateTo(res.getStart().pt);
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(WebSiteRouteActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(WebSiteRouteActivity.this, mMapView);
			    // 此处仅展示一个方案作为示例
			    routeOverlay.setData(res.getPlan(0).getRoute(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    mMapView.invalidate();
			    
			    mMapView.getController().animateTo(res.getStart().pt);
			    
			}
			public void onGetAddrResult(MKAddrInfo res, int error) {
			}
			public void onGetPoiResult(MKPoiResult res, int arg1, int arg2) {
			}
			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
				// TODO Auto-generated method stub
				
			}

            @Override
            public void onGetRGCShareUrlResult(String arg0, int arg1) {
                // TODO Auto-generated method stub
                
            }
        });
        btTopLeft_wsr = (Button) findViewById(R.id.btTopLeft_wsr);
        btTopLeft_wsr.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        getRoute();
		
	}

	


	@Override
	protected void onPause() {
		
		if (HomeActivityGroup.mBMapMan != null) {
			HomeActivityGroup.mBMapMan.stop();
		}
		WebSiteActivity.mMapView.getController().setZoom(WebSiteActivity.zoomLevel);
		WebSiteActivity.mMapView.getController().setCenter(new GeoPoint((int)(Double.parseDouble(this.getIntent().getExtras().getString("yCoordinates")) * 1e6), 
				(int)(Double.parseDouble(this.getIntent().getExtras().getString("xCoordinates")) * 1e6)));
//		System.out.println(this.getIntent().getExtras().getString("yCoordinates")+"-++onPause+++++++++++-----------"+this.getIntent().getExtras().getString("xCoordinates"));
		
		MobclickAgent.onPause(this);
		super.onPause();
		
//		
		
	}

	@Override
	protected void onResume() {
		if (HomeActivityGroup.mBMapMan != null) {
			HomeActivityGroup.mBMapMan.start();
		}
		MobclickAgent.onResume(this);
		super.onResume();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	void getRoute() {
		// 对起点终点的name进行赋值，也可以直接对坐标赋值，赋值坐标则将根据坐标进行搜索
		MKPlanNode stNode = new MKPlanNode();
//		stNode.pt = new GeoPoint((int)(30.612651*
//				 1e6), (int)(114.220378 * 1e6));
		if(WebSiteActivity.locData==null)
		{
			Toast.makeText(WebSiteRouteActivity.this, "抱歉，无法定位当前位置", Toast.LENGTH_SHORT).show();
			finishProgress();
			return;
		}
		stNode.pt = new GeoPoint((int)(WebSiteActivity.locData.getLatitude()*
				 1e6), (int)(WebSiteActivity.locData.getLongitude() * 1e6));
		MKPlanNode enNode = new MKPlanNode();
//		System.out.println(this.getIntent().getExtras().getString("yCoordinates")+"-++++++++++++++-----------"+this.getIntent().getExtras().getString("xCoordinates"));
		enNode.pt = new GeoPoint((int)(Double.parseDouble(this.getIntent().getExtras().getString("yCoordinates")) * 1e6), 
				(int)(Double.parseDouble(this.getIntent().getExtras().getString("xCoordinates")) * 1e6));
		// 实际使用中请对起点终点城市进行正确的设定
		mSearch.drivingSearch(null, stNode, null, enNode);
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	protected void showProgress(String message) {
		progressDialog = new ProgressDialog(WebSiteRouteActivity.this);
		progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		progressDialog.setMessage(message);
		progressDialog.show();
	}
	
	/**
	 * 结束进度框
	 * 
	 * @param progress
	 */
	protected void finishProgress() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog=null;
		}
	}
}




